const express = require("express");
const getSendData = require("../utils/getSendData");
const blogServ = require("../services/blogService");

const router = express.Router();

router.post("/", async (req, res, next) => {
    res.send(getSendData(await blogServ.addBlog(req.body)));
});

router.get("/", async (req, res, next) => {
    res.send(getSendData(await blogServ.getBlogs(+req.query.page, +req.query.limit, req.query.categoryId ? +req.query.categoryId : -1)));
});

router.get("/:id", async (req, res, next) => {
    res.send(getSendData(await blogServ.getBlogById(req.params.id, req.headers.authorization)));
});

router.put("/:id", async (req, res, next) => {
    res.send(getSendData(await blogServ.updateBlog(req.params.id, req.body)));
});

router.delete("/:id", async (req, res, next) => {
    res.send(getSendData(await blogServ.deleteBlog(req.params.id)));
});

module.exports = router;
